import {useSetUrlSearchParam, useUrlQueryParam} from "../../utils/url";
import {useMemo} from "react";
import {useProject} from "../../utils/project";

// 项目列表搜索的参数
export const useProjectsSearchParams = () => {
    const [param, setParam] = useUrlQueryParam(['name', 'personId'])
    return [
        useMemo(() => ({...param, personId: Number(param.personId)}), [param]),
        setParam
    ] as const
}

export const useProjectsQueryKey = () => {
    const [params] = useProjectsSearchParams()
    return ['projects', params]
}

export const useProjectModal = () => {
    const [{projectCreate}, setProjectCreate] = useUrlQueryParam([
        'projectCreate'
    ])

    const [{editingProjectId}, setEditingProjectId] = useUrlQueryParam([
        'editingProjectId'
    ])

    const setSearchParams = useSetUrlSearchParam()

    const {data: editingProject, isLoading} = useProject(Number(editingProjectId))

    const open = () => setProjectCreate({projectCreate: true})
    const close = () => setSearchParams({projectCreate: '', editingProjectId: ''})
    const startEdit = (id: number) => setEditingProjectId({editingProjectId: id})
    return {
        projectModalOpen: projectCreate === 'true' || Boolean(editingProjectId),
        open,
        close,
        startEdit,
        editingProject,
        isLoading
    }
}
